﻿// SilverlightDesktop.Net - http://www.SilverlightDesktop.net
// Copyright (c) 2008
// by SilverlightDesktop.Net
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using WhoAmI.WhoamI;
using System.ServiceModel;
using System.ServiceModel.Channels;

namespace WhoAmI
{
    public partial class Page : UserControl
    {
        public SilverlightDesktop.Page ParentPage;

        public int intPortalID;
        public int intModuleId;
        public int intUserID;
        public string strPassword;
        public string strWebServiceBase;

        public Page()
        {
            // Required to initialize variables
            InitializeComponent();
            Loaded += new RoutedEventHandler(Page_Loaded);
        }

        #region Page_Loaded
        void Page_Loaded(object sender, RoutedEventArgs e)
        {
            // Get parameters from the Tag
            char[] delimiterChars = { ',' };
            string[] strParameters = this.Tag.ToString().Split(delimiterChars);

            intPortalID = Convert.ToInt32(strParameters[0]);
            intModuleId = Convert.ToInt32(strParameters[1]);
            intUserID = Convert.ToInt32(strParameters[2]);
            strPassword = strParameters[3];
            strWebServiceBase = strParameters[4];

            ShowWhoAmI();
        }
        #endregion

        #region ShowWhoAmI
        private void ShowWhoAmI()
        {
            BasicHttpBinding bind = new BasicHttpBinding();
            EndpointAddress MyEndpointAddress = new EndpointAddress(strWebServiceBase + "WhoAmI.asmx");
            var proxy = new WhoAmISoapClient(bind, MyEndpointAddress);

            proxy.WhoIAmCompleted += new EventHandler<WhoIAmCompletedEventArgs>(proxy_WhoIAmCompleted);
            proxy.WhoIAmAsync(intPortalID, intModuleId, intUserID, strPassword);
        }

        void proxy_WhoIAmCompleted(object sender, WhoIAmCompletedEventArgs e)
        {
            UserInfo UserInfo = (UserInfo)e.Result;
            DisplayWhoIAm(UserInfo);
        }

        private void DisplayWhoIAm(UserInfo UserInfo)
        {
            this.txtFirstName.Text = UserInfo.FirstName;
            this.txtLastName.Text = UserInfo.LastName;
            this.txtEmail.Text = UserInfo.Email;
        }
        #endregion

        #region AddDelegates
        public void AddDelegates(SilverlightDesktop.Page objParentPage)
        {
            ParentPage = objParentPage;

            // Add delegates to the parent to allow it to handle the drag and drop
            FrameworkElement objFrameworkElementFirstName = (FrameworkElement)this.txtFirstName;
            objFrameworkElementFirstName.MouseLeftButtonDown +=
                new MouseButtonEventHandler(ParentPage.objFrameworkElement_MouseLeftButtonDown);

            FrameworkElement objFrameworkElementLastName = (FrameworkElement)this.txtLastName;
            objFrameworkElementLastName.MouseLeftButtonDown +=
                new MouseButtonEventHandler(ParentPage.objFrameworkElement_MouseLeftButtonDown);

            FrameworkElement objFrameworkElementEmail = (FrameworkElement)this.txtEmail;
            objFrameworkElementEmail.MouseLeftButtonDown +=
                new MouseButtonEventHandler(ParentPage.objFrameworkElement_MouseLeftButtonDown);
        } 
        #endregion

        #region Text_MouseEnter
        private void Text_MouseEnter(object sender, MouseEventArgs e)
        {
            TextBlock objTextBlock = (TextBlock)e.OriginalSource;
            objTextBlock.Foreground = new SolidColorBrush(Colors.Red);
        } 
        #endregion

        #region Text_MouseLeave
        private void Text_MouseLeave(object sender, MouseEventArgs e)
        {
            TextBlock objTextBlock = (TextBlock)e.OriginalSource;
            objTextBlock.Foreground = new SolidColorBrush(Colors.Black);
        } 
        #endregion
    }
}
